package com.dbb.util;

import java.util.ArrayList;
import java.util.List;

public class StringUtils {
	public static String htmlspecialchars(String str) {
		str = str.replaceAll("&", "&amp;");
		str = str.replaceAll("<", "&lt;");
		str = str.replaceAll(">", "&gt;");
		str = str.replaceAll("\"", "&quot;");
		return str;
	}
	
	public static String getTableName(Class c) {
		String cn = c.getName().substring(c.getName().lastIndexOf("."));
		return cn.toUpperCase();
	}
	
	public static String[] getArrayFromString(String str, String regex) {
		if(str == null ) {
			return null;
		}
		if(str.startsWith(regex)) {
			str = str.substring(1);
		}
		if(str.endsWith(regex)) {
			str = str.substring(0, str.length() -1);
		}
		String[] imgIds = str.split(regex); 
		return imgIds;
	}
	public static String REGEX = " ";
	public static List<String> getUnitListString(String str, String... regex) {
		String s =  str;
		for(String reg : regex) {
			s = str.replaceAll(reg, REGEX);
		}
		return getUnitListString(s, REGEX);
	}

	public static List<String> getUnitListString(String str, String regex) {
		List<String> list = new ArrayList<String>();
		if(str.startsWith(regex)) {
			str = str.substring(1);
		}
		if(str.endsWith(regex)) {
			str = str.substring(0, str.length() -1);
		}
		String[] strs = str.split(regex);
		for(String s : strs) {
			if(!list.contains(s)) {
				list.add(s);
			}
		}
		return list;
	}
}
